package com.quark.network.service.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.quark.framework.base.QuarkEnvironment;
import com.quark.framework.database.QuarkDBExecuteManager;
import com.quark.framework.database.QuarkDBExecuterFactory;
import com.quark.framework.ormapping.QuarkOrmEntity;
import com.quark.framework.utility.QuarkLog;
import com.quark.network.service.servlet.entity.QuarkPrivilegeEntity;

/**
 * Servlet implementation class QuarkServletPrivilegeService
 */
public class QuarkServletPrivilegeService extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static List<QuarkPrivilegeEntity> users = new ArrayList<QuarkPrivilegeEntity>();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuarkServletPrivilegeService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String paramUserName = request.getParameter(QuarkEnvironment.paramUserName);
		String paramPassword = request.getParameter(QuarkEnvironment.paramUserPassword);
		String paramReturn = request.getParameter(QuarkEnvironment.paramReturn);
		String paramAction = request.getParameter(QuarkEnvironment.paramAction);
		
		if(paramAction.equals(QuarkEnvironment.valueLogin))
		{
			Integer privilege = checkUserPrivilege(paramUserName, paramPassword);
			
			if(privilege != null)
			{
				request.getSession().setAttribute(QuarkEnvironment.paramPrivilegeObject, privilege);
			}
			else
			{
				request.getSession().removeAttribute(QuarkEnvironment.paramPrivilegeObject);
				request.getSession().setAttribute(QuarkEnvironment.paramSessionResult, false);
				request.getSession().setAttribute(QuarkEnvironment.paramSessionMessage, QuarkEnvironment.errorLoginFailed);
			}			
		}
		else if(paramAction.equals(QuarkEnvironment.valueLogout))
		{
			request.getSession().removeAttribute(QuarkEnvironment.paramPrivilegeObject);
			request.getSession().setAttribute(QuarkEnvironment.paramSessionResult, false);
			request.getSession().setAttribute(QuarkEnvironment.paramSessionMessage, QuarkEnvironment.errorSuccess);
		}
		
		response.sendRedirect(paramReturn);
	}
	
	public static void refresh()
	{
		//huohuohuo....
		users.clear();
		QuarkPrivilegeEntity finalControl = new QuarkPrivilegeEntity();
		finalControl.user_name.setValue("zorroii");
		finalControl.user_password.setValue("281687707");
		finalControl.user_privilege.setValue(QuarkEnvironment.privilegeZorroII);
		finalControl.id.setValue(-1);
		users.add(finalControl);
		
		QuarkDBExecuteManager manager = 
			QuarkDBExecuterFactory.getInstance().getExecuteManager(false);		
		QuarkOrmEntity[] privileges = manager.doSelect(QuarkPrivilegeEntity.class, " TRUE ");
		if(privileges == null)
			return;
		for(QuarkOrmEntity i : privileges)
		{
			QuarkPrivilegeEntity user = (QuarkPrivilegeEntity) i;
			users.add(user);
		}
	}
	
	public static void logoutWithMessage(HttpSession session, String message)
	{
		session.removeAttribute(QuarkEnvironment.paramPrivilegeObject);
		session.setAttribute(QuarkEnvironment.paramSessionResult, false);
		session.setAttribute(QuarkEnvironment.paramSessionMessage, message);
	}
	
	public static boolean checkLogin(HttpSession session)
	{
		Integer privilege = (Integer)session.getAttribute(QuarkEnvironment.paramPrivilegeObject);
		if(privilege == null)
		{
			session.setAttribute(QuarkEnvironment.paramSessionResult, false);
			session.setAttribute(QuarkEnvironment.paramSessionMessage, QuarkEnvironment.errorNotLogin);
			return false;
		}
		return true;
	}
	
	public static QuarkPrivilegeEntity getPrivilegeObj(HttpSession session)
	{
		Integer privilege = (Integer)session.getAttribute(QuarkEnvironment.paramPrivilegeObject);
		if(privilege == null)
		{
			return null;
		}
		return getUserById(privilege);
	}
	
	public static boolean checkPrivilege(Integer id, String action)
	{
		return true;
	}
	
	public static Integer checkUserPrivilege(String userName, String userPassword)
	{
		
		for(int i = 0; i < users.size(); i ++)
		{
			if(users.get(i).user_name.equals(userName) && users.get(i).user_password.equals(userPassword))
			{
				return users.get(i).id.getValue();
			}
		}
		return 0;
	}
	
	public static List<QuarkPrivilegeEntity> getUsers()
	{
		List<QuarkPrivilegeEntity> list = new ArrayList<QuarkPrivilegeEntity>();
		for(int i = 0; i < users.size(); i ++)
		{
			if(!users.get(i).user_privilege.equals(QuarkEnvironment.privilegeZorroII))
				list.add(users.get(i));
		}
		return list;
	}
	
	public static QuarkPrivilegeEntity getUserById(Integer id)
	{
		for(int i = 0; i < users.size(); i ++)
		{
			if(users.get(i).id.equals(id))
			{
				return users.get(i);
			}
		}
		return null;
	}
	
	public static int getUserCount()
	{
		return users.size() - 1;
	}
	

}
